package com.geek.gkfood.biz.facade.meal.action.query;

import com.geek.gkfood.biz.util.RespUtils;
import com.geek.gkfood.facade.meal.bean.QuerySnatchOrderListReq;
import com.geek.gkfood.facade.meal.bean.QuerySnatchOrderListResp;
import com.geek.gkfood.facade.meal.bean.SnatchOrderItem;
import com.geek.gkfood.framework.redis.RedisOperator;
import com.geek.gkfood.service.redis.SnatchOrderLuaExecutor;
import com.geek.gkfood.util.collection.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <h1>查询抢单列表处理类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/29 08:26
 */
@Component
public class QuerySnatchOrderListAction {

    @Autowired
    private SnatchOrderLuaExecutor snatchOrderLuaExecutor;

    /**
     * <h2>查询抢单列表。</h2>
     *
     * @param: req
     * @return: com.geek.gkfood.facade.meal.bean.QuerySnatchOrderListResp
     */
    public QuerySnatchOrderListResp querySnatchOrderList(QuerySnatchOrderListReq req) {
        // 先查 redis
        List<SnatchOrderItem> redisSnatchOrderList = queryFromRedis(req.getCurrUserId());

        // redis 没有再查 db
        List<SnatchOrderItem> dbSnatchOrderList = queryFromDb(req.getCurrUserId());

        // 返回成功响应
        return buildSuccResp(redisSnatchOrderList, dbSnatchOrderList);
    }

    private QuerySnatchOrderListResp buildSuccResp(List<SnatchOrderItem> redisSnatchOrderList,
                                                   List<SnatchOrderItem> dbSnatchOrderList) {
        QuerySnatchOrderListResp resp = new QuerySnatchOrderListResp();
        resp.setSnatchOrderItemList(CollectionUtils.mergeList(redisSnatchOrderList, dbSnatchOrderList));

        return RespUtils.ok(resp);
    }

    private List<SnatchOrderItem> queryFromRedis(String currUserId) {
        return RedisOperator.hgetAll(
                snatchOrderLuaExecutor.getKeys2(currUserId),
                SnatchOrderItem.class);
    }

    private List<SnatchOrderItem> queryFromDb(String currUserId) {
        return null;
    }
}